package com.cobo.fitness.web.fitness.vo;

import com.cobo.fitness.web.fitness.entity.Instruction;
import com.cobo.fitness.web.fitness.entity.SecondaryMuscle;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class ExerciseVo {

    private String bodyPart;

    private String equipment;

    private String gifUrl;

    private String id;

    private String name;

    private String target;

    private String[] secondaryMuscles;

    private String[] instructions;

    private byte[] giftBlob;

    private List<Instruction> instructionList;

    private List<SecondaryMuscle> secondaryMuscleList;

    public void transform() {
        this.secondaryMuscleList = Arrays.stream(this.secondaryMuscles).map(it -> {
            SecondaryMuscle item = new SecondaryMuscle();
            item.setExerciseId(this.id);
            item.setContent(it);
            return item;
        }).collect(Collectors.toList());

        this.instructionList = Arrays.stream(this.instructions).map(it -> {
            Instruction item = new Instruction();
            item.setExerciseId(this.id);
            item.setContent(it);
            return item;
        }).collect(Collectors.toList());

        String fname = StringUtils.substringAfterLast(this.gifUrl, "/") + ".gif";
        File file = new File("D:\\fitness\\asserts", fname);
        if (file.exists()) {
            try {
                this.giftBlob = FileUtils.readFileToByteArray(file);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
